package com.wc.算法提高课.C第三章_图论.最小生成树的扩展应用.新的开始;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/16 21:05
 * @description https://www.acwing.com/problem/content/1148/
 */
public class Main {
    /**
     * 思路；虚拟原点<p>
     * 原点连接每个点的代价是vi<p>
     * 相当于需要连接至少n条边, 将所有点连接起来, 供电
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 310, INF = 0x3f3f3f3f;
    static int[] dist = new int[N];
    static int[][] w = new int[N][N];
    static boolean[] st = new boolean[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) w[0][i] = w[i][0] = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                w[i][j] = sc.nextInt();
            }
        }
        out.println(prim());
        out.flush();
    }

    static int prim() {
        int res = 0;
        Arrays.fill(dist, 0, n + 1, INF);
        dist[0] = 0;
        for (int i = 0; i <= n; i++) {
            int t = -1;
            for (int j = 0; j <= n; j++) {
                if (!st[j] && (t == -1 || dist[t] > dist[j])) {
                    t = j;
                }
            }
            st[t] = true;
            res += dist[t];
            for (int j = 0; j <= n; j++) dist[j] = Math.min(dist[j], w[t][j]);
        }
        return res;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
